#!/bin/bash
# Version:1.0.0
# Author:modongxiao
# Description:memcache
# chmod +x memcache.sh

# 错误监测
# 参数（在执行后执行函数，并传入错误信息）
# 第一个参数：固定 $?
# 第二个参数：错误信息
# 例如：check_error $? "执行错误"
function check_error {
     if [ $1 -ne 0 ] ; then
        echo $2",请检查！";
        exit 1
    fi
    return 0;
}

yum -y install memcached libevent
service memcached start

case "$1" in
    install)
        # if [ -d "/usr/local/nginx" ] ; then
        #     echo "nginx可能已经安装，请查看 /usr/local/ 目录下的 nginx 目录";
        #     exit 1
        # fi
        # if [ ! -d "/data/log/nginx" ] ; then
        #     mkdir -pv /data/log/nginx
        #     echo "日志目录创建完成";
        # fi
        # if [ ! -d "/var/cache/nginx/client_temp" ] ; then
        #     mkdir -pv /var/cache/nginx/client_temp
        # fi
        # yum -y install wget tar
        # yum -y install gcc gcc-c++  pcre-devel  openssl-devel
        # check_error $? "yum 安装依赖包错误"

        # 下载 与 编译安装
        # id nginx || useradd -r nginx
        mkdir memcache
        cd memcache
        wget http://172.18.202.1/source/memcache/memcached-1.4.26.tar.gz
        check_error $? "安装包下载错误"
        tar -axf memcached-1.4.26.tar.gz
        cd memcached-1.4.26
        check_error $? "安装包路径错误"
        ./configure --prefix=/usr/local/memcache --sysconfdir=/etc/memcache/ 
        check_error $? "configure 编译错误"
        make -j 4
        check_error $? "make 编译错误"
        make install
        check_error $? "make install 编译错误"
        echo "编译安装完成...开始配置..."

        # 配置
        # echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh;
        # source /etc/profile.d/nginx.sh
        # rm -rf  /etc/nginx/nginx.conf
        # wget http://172.18.202.1/source/nginx/conf/nginx.conf -P /etc/nginx/
        # rm -rf  /etc/nginx/nginx.d/magedu.conf
        # wget http://172.18.202.1/source/nginx/conf/magedu.conf -P /etc/nginx/nginx.d/
        # [ ! -f /etc/rc.d/init.d/nginx ] && wget http://172.18.202.1/source/nginx/conf/nginx -P /etc/rc.d/init.d/
        # chmod +x /etc/rc.d/init.d/nginx
        # chkconfig --add nginx
        # chkconfig --level 345 nginx on
        # # 启动
        # service nginx start
        # check_error $? "nginx 启动错误"
        # echo "nginx 启动完成!";
    ;;
    delete)
        service nginx stop
        rm -rf /usr/local/nginx/
        rm -rf /etc/nginx/
        rm -rf /etc/rc.d/init.d/nginx
        echo '卸载完成 ! '
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {install|start|shutdown|reboot|delete|}'
        exit 1
    ;;
esac





